package com.lsa.cormen.ch15.samples;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileFilter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Arrays;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.io.FilenameUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.io.filefilter.WildcardFileFilter;
import org.apache.commons.lang.text.StrSubstitutor;

import com.google.common.base.Function;
import com.google.common.base.Predicate;
import com.google.common.collect.FluentIterable;
import com.google.common.collect.Lists;
import com.google.common.primitives.Ints;

public class SubsetSumTestTranslator {

    /**
     * @param args
     * @throws IOException 
     * @throws FileNotFoundException 
     */
    @SuppressWarnings("unchecked")
    public static void main(String[] args) throws FileNotFoundException, IOException {
        File dir = new File("x:\\distrib\\inet\\wget\\here\\people.sc.fsu.edu\\%7Ejburkardt\\datasets\\subset_sum");
        FileFilter fileFilter = new WildcardFileFilter("*_c.txt");
        File[] files = dir.listFiles(fileFilter);
        Arrays.sort(files, new Comparator<File>() {
            @Override
            public int compare(File o1, File o2) {
                return o1.getName().compareToIgnoreCase(o2.getName());
            }
        });
        for (int i = 1; i < files.length; i++) {
            File mainFile = files[i];
            List<String> lines = readLines(mainFile);
            int capacity = Integer.parseInt(lines.get(0).trim());
            int[] weigths;
            Integer[] selected;
            
            {
                String weightFileName = FilenameUtils.concat(mainFile.getParent(), mainFile.getName().replace("_c", "_w"));
                lines = readLines(new File(weightFileName));
                
                weigths = Ints.toArray(Lists.transform(lines, new Function<String, Integer>() {
                    @Override
                    public Integer apply(String s) {
                        return Integer.parseInt(s.trim());
                    }
                }));
            }
            
            {
                String weightFileName = FilenameUtils.concat(mainFile.getParent(), mainFile.getName().replace("_c", "_s"));
                lines = readLines(new File(weightFileName));
                
                selected = FluentIterable.from(Lists.transform(lines,
                        new Function<String, Integer>() {
                            int cnt = -1;

                            @Override
                            public Integer apply(String s) {
                                int i = Integer.parseInt(s.trim());
                                cnt++;
                                if (i == 1) {
                                    return cnt;
                                }
                                return -1;
                            }
                        })).filter(new Predicate<Integer>() {

                            @Override
                            public boolean apply(Integer m) {
                                return m > -1;
                            }
                        }).toArray(Integer.class);
            }
            
            String template = IOUtils.toString(SubsetSumTestTranslator.class.getResourceAsStream("SubsetSumTestTemplate.txt"));
            
            @SuppressWarnings("rawtypes")
            Map valuesMap = new HashMap();
            valuesMap.put("testNo", i+1);
            valuesMap.put("capacity", capacity);
            valuesMap.put("weigths", toStr(Ints.asList(weigths)));
            valuesMap.put("selected", toStr(Arrays.asList(selected)));
            
            StrSubstitutor sub = new StrSubstitutor(valuesMap);
            System.out.println(sub.replace(template));
        }
    }
    
    private static <T> String toStr(List<T> arr) {
        String s = ""+arr;
        return s.replace("[", "").replace("]", "");
    }

    public static List<String> readLines(File f) throws IOException,
            FileNotFoundException {
        return IOUtils.readLines(new BufferedReader(new FileReader(f)));
    }
}
